package com.bootdo.system.service.impl;

import com.bootdo.common.config.BootdoConfig;
import com.bootdo.system.dao.DlNewsCategoryDao;
import com.bootdo.system.domain.DlNewsCategoryDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.system.dao.DlNewsDao;
import com.bootdo.system.domain.DlNewsDO;
import com.bootdo.system.service.DlNewsService;



@Service
public class DlNewsServiceImpl implements DlNewsService {
	@Autowired
	private DlNewsDao dlNewsDao;
	@Autowired
	private DlNewsCategoryDao dlNewsCategoryDao;
	@Autowired
	BootdoConfig bootdoConfig;

	@Override
	public DlNewsDO get(Integer newsId){
		return dlNewsDao.get(newsId);
	}
	
	@Override
	public List<DlNewsDO> list(Map<String, Object> map){
		List<DlNewsDO> list = dlNewsDao.list(map);
		for (DlNewsDO dlNewsDO : list) {
			DlNewsCategoryDO dlNewsCategoryDO = dlNewsCategoryDao.get(dlNewsDO.getNewsCategoryId());
			dlNewsDO.setNewsCategoryName(dlNewsCategoryDO.getTitle());
		}
		return list;
	}
	
	@Override
	public int count(Map<String, Object> map){
		return dlNewsDao.count(map);
	}
	
	@Override
	public int save(DlNewsDO dlNews){
		dlNews.setAuthor(bootdoConfig.getUsername());
		dlNews.setPubTime(new Date());
		dlNews.setAuditStatus("1");
		dlNews.setVoteNum(0);
		dlNews.setHits(0);
		dlNews.setSort(0);
		return dlNewsDao.save(dlNews);
	}
	
	@Override
	public int update(DlNewsDO dlNews){
		if(dlNews.getTitlePhoto() != null){
			String[] split = dlNews.getTitlePhoto().split("/");
			dlNews.setTitlePhoto(split[split.length-1]);
		}
		if(dlNews.getBannerPhoto() != null){
			String[] split = dlNews.getBannerPhoto().split("/");
			dlNews.setBannerPhoto(split[split.length-1]);
		}
		if(dlNews.getCarouselPhoto() != null){
			String[] split = dlNews.getCarouselPhoto().split("/");
			dlNews.setCarouselPhoto(split[split.length-1]);
		}
		return dlNewsDao.update(dlNews);
	}
	
	@Override
	public int remove(Integer newsId){
		return dlNewsDao.remove(newsId);
	}
	
	@Override
	public int batchRemove(Integer[] newsIds){
		return dlNewsDao.batchRemove(newsIds);
	}
	
}
